#include<p24fj128ga006.h>
#include <libpic30.h>
#include "dba.h"


void dba(void)
{
    int i,z,j;    
    unsigned char temp[30];    
    unsigned char *temp_store;
	//unsigned long PrevLengthTime;
 
      for(i=0;i<onu_ctr2;i++)
      {
		    if(report_list[i][0]==0x00)// report empty
		  {
			  grants_list[i][0] = 0x01; // data set
			  grants_list[i][1] = 0x01; // no of gtants/flag
    
			  x = changeByte2Long(grants_list[i-1][2],grants_list[i-1][3],grants_list[i-1][4],grants_list[i-1][5]);  //time
			  //PrevLengthTime = changeByte2Long(grants_list[i-1][6],grants_list[i-1][7],0,0);  //for length
			  
			  __delay32(10000);
			  grant_alc =  x + 750;   //1 tick = 40us (appicable for 1500 only)
			       
			  temp_store = changeLong2Byte(grant_alc);
			  grants_list[i][2] = temp_store[0];  //Q1[31] GATE start time
              grants_list[i][3] = temp_store[1];  //Q1[32] GATE start time
			  grants_list[i][4] = temp_store[2];  //Q1[33] GATE start time
			  grants_list[i][5] = temp_store[3];  //Q1[34] GATE start time 

              //length
              grants_list[i][6] = 0x02; // 750
              grants_list[i][7] = 0xEE; 
              //check status
			         
		  }
		  else if(report_list[i][0]==0x01)
		  {
			  grants_list[i][0] = 0x01; // data set
			  grants_list[i][1] = 0x01;   //no of grant - 1 grant

			  //start time
			  x = changeByte2Long(grants_list[i-1][2],grants_list[i-1][3],grants_list[i-1][4],grants_list[i-1][5]); //prev time without length.
				  
				  //PrevLengthTime = changeByte2Long(grants_list[i-1][6],grants_list[i-1][7],0,0); //fetch prev grant_list length 	-change big to little
			  //x = x + PrevLengthTime;  // prev time+ length
				  	
				  grant_alc = x + 900;   //grant length
				 
				  temp_store = changeLong2Byte(grant_alc); 
				  grants_list[i][2] = temp_store[0];  //Q1[31] GATE start time
                  grants_list[i][3] = temp_store[1];  //Q1[32] GATE start time
				  grants_list[i][4] = temp_store[2];  //Q1[33] GATE start time
				  grants_list[i][5] = temp_store[3];  //Q1[34] GATE start time
                  

				  //length
				  grants_list[i][6] = 0x03;  // 900 for grant length
				  grants_list[i][7] = 0x84; 
			 
		   	
		  }
	  //}
	  
	      //send grant
          for(z=0;z<onu_ctr2;z++)//No of onu
          {       
               for(j=0;j<25;j++)//copy grant to mpcp
               {
                   temp[j] = grants_list[z][j+1];
                   grants_list[z][j] = 0x00; //clr buffer after transfer
               }
		       grants_reg_status=0x03;                        
               GATE_Normal(z,0x00,temp);//send gate for alive
		       grants_reg_status=0x03;  
               // capture_delay(0x02);//capture and send   
    
              ///TX2EN = 0;//enable uart module  
           }
   
      }
}
